package com.tsystems.school.EntranceApp1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Collections;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Main {

    /*
     * @param args First argument is a name of input file. Second argument is an output destination.
     */
    public static void main(String[] args) {
    	PropertyConfigurator.configure("resources/properties/log4j.properties");
    	Logger log = Logger.getLogger("Main Logger:");
        if(args.length < 1) {
        	log.error("Not enough parameters (2 needed)");
            return;
        }
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        // Reading
        try {
            BufferedReader reader = new BufferedReader(new FileReader(args[0]));
            String line;
            try {
                while ((line = reader.readLine()) != null) {
                    line = line.trim();
                    if (map.containsKey(line)) {
                        map.put(line, 1 + map.get(line));
                    } else {
                        map.put(line, 1);
                    }
                }
            } catch (IOException exception) {
            	log.error("Unable to read file.");
            	return;
            }
            try {
                reader.close();
            } catch (IOException exception) {
            	log.warn("Unable to close file " + args[0]);
            }
        } catch (FileNotFoundException exception) {
            // In case if file not found
            log.error("File " + args[0] + " does not exist.");
            return;
        }
        // Writing
        String destination = "";
        if (args.length > 1) {
            destination = args[1];
            destination = destination.replace('\"', '\\');
            if(!destination.endsWith("\\")) {
                destination += "\\";
            }
        } else {
        	log.warn("No output destination. Output will be placed in current location.");
        }
        try {
            BufferedWriter writer1 =
                   new BufferedWriter(new FileWriter(destination + "1out.txt"));
            BufferedWriter writer2 =
                   new BufferedWriter(new FileWriter(destination + "2out.txt"));
            ArrayList<String> list = new ArrayList<String>(map.keySet());
            Collections.sort(list);
            for (String string : list) {
                writer1.write(string);
                writer1.newLine();
                int amount = map.get(string);
                if (amount > 1) {
                    writer2.write(string + " - " + amount);
                    writer2.newLine();
                }
            }
            writer1.close();
            writer2.close();
        } catch (IOException exception) {
        	log.error("Cannot create file.");
        }
        log.info("Output files successfully created.");
    }
    
}
